// @Author : zaniu(zzaniu@126.com)
// @Time   : 2021/9/6 20:36
// @Desc   :
//
//                   _ooOoo_
//                  o8888888o
//                  88" . "88
//                  (| -_- |)
//                  O\  =  /O
//               ____/`---'\____
//             .'  \\|     |//  `.
//            /  \\|||  :  |||//  \
//           /  _||||| -:- |||||-  \
//           |   | \\\  -  /// |   |
//           | \_|  ''\---/''  |   |
//           \  .-\__  `-`  ___/-. /
//         ___`. .'  /--.--\  `. . __
//      ."" '<  `.___\_<|>_/___.'  >'"".
//     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//     \  \ `-.   \_ __\ /__ _/   .-` /  /
// =====`-.____`-.___\_____/___.-`____.-'=====
//                   `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//           佛祖保佑       永无BUG
//
package nacos

import (
	"distributedTransaction/utils"
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

var clientConfig = constant.ClientConfig{
	// we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
	NamespaceId:          utils.NamespaceId,
	TimeoutMs:            5000,
	NotLoadCacheAtStart:  true,
	LogDir:               "utils/nacos/tmp/log",
	CacheDir:             "/utils/nacos/tmp/cache",
	RotateTime:           "24h",
	MaxAge:               3,
	LogLevel:             "debug",
	UpdateCacheWhenEmpty: false, // 当service返回的实例列表为空时，不更新缓存，用于推空保护
}

var serverConfigs = []constant.ServerConfig{
	{
		IpAddr:      "120.236.12.126",
		ContextPath: "/nacos",
		Port:        8848,
		Scheme:      "http",
	},
}

func getNewNamingClient() (naming_client.INamingClient, error) {
	namingClient, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &clientConfig,
			ServerConfigs: serverConfigs,
		},
	)
	if err != nil {
		fmt.Println("clients.NewNamingClient error, err = ", err.Error())
		return nil, err
	}
	return namingClient, nil
}

func getConfigClient() (config_client.IConfigClient, error) {
	configClient, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &clientConfig,
			ServerConfigs: serverConfigs,
		},
	)
	if err != nil {
		fmt.Println("clients.NewConfigClient error, err = ", err.Error())
		return nil, err
	}
	return configClient, nil
}
